package org.csu.tank.collision;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.physics.CollisionHandler;
import org.csu.tank.GameConfig;
import org.csu.tank.GameType;
import org.csu.tank.ItemType;
import org.csu.tank.components.DataComponent;
import org.csu.tank.components.EnemyComponent;
import org.csu.tank.components.PlayerComponent;

import java.util.concurrent.atomic.AtomicBoolean;

import static com.almasb.fxgl.dsl.FXGL.*;
import static com.almasb.fxgl.dsl.FXGL.getAppHeight;

/**
 * 子弹和敌人的碰撞(需要忽略敌人阵营的子弹;这个忽略的设置写到了产生子弹的实体方法里了)
 *      销毁敌人 也 销毁子弹
 */
public class BulletEnemyHandler extends CollisionHandler {

    public BulletEnemyHandler() {
        super(GameType.BULLET, GameType.ENEMY);
    }

    protected void onCollisionBegin(Entity bullet, Entity enemy) {
        AtomicBoolean isCollision= new AtomicBoolean(true);
        bullet.getComponentOptional(DataComponent.class).ifPresent(dataComponent -> {
//                  System.out.println("存储了组件的id" + dataComponent.getId());
            if (dataComponent.getId() == enemy.getComponent(EnemyComponent.class).getId()) {
//                System.out.println("id 相同不碰撞");
                isCollision.set(false);
            }
        });
        if(isCollision.get()==false)
            return;
        play("normalBomb.wav");
        spawn("explode", enemy.getCenter().getX() - 25, enemy.getCenter().getY() - 20);
        bullet.removeFromWorld();
        enemy.removeFromWorld();
        inc("destroyedEnemy", 1);
        // get item 一定几率得到道具
        if (FXGLMath.randomBoolean(GameConfig.SPAWN_ITEM_PRO)) {
            spawn("item",
                    new SpawnData(FXGLMath.random(50, getAppWidth() - 50 - 6 * 24)
                            , FXGLMath.random(50, getAppHeight() - 50))
                            .put("itemType", FXGLMath.random(ItemType.values()).get()));
        }
    }
}
